package com.stedu.service.impl;

import com.stedu.bean.Teacher;
import com.stedu.exception.SteduException;
import com.stedu.mapper.TeacherAndStudentMapper;
import com.stedu.mapper.TeacherMapper;
import com.stedu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private TeacherAndStudentMapper teacherAndStudentMapper;
    @Override
    public List<Teacher> selectByTname(String tname) {
        return teacherMapper.selectByTname(tname);
    }

    @Override
    public boolean insert(Teacher t) throws SteduException {
        if (teacherMapper.selectByTname(t.getTname())==null){
            //抛出自定义异常
            throw new SteduException("该老师已经存在，无法重复添加");
        }
        return teacherMapper.insert(t)==1;
    }

    @Override
    public boolean delete(Integer tid) throws SteduException {
        if(!teacherAndStudentMapper.selectSidByTid(tid).isEmpty()){
            return false;
        }
        return teacherMapper.delete(tid) == 1;
    }

    @Override
    public boolean update(Teacher t) throws SteduException {
        if (teacherMapper.selectByTid(t.getTid())==null){
            throw new SteduException("该学生不存在，无法修改");
        }
        return teacherMapper.update(t)==1;
    }

    @Override
    public List<Teacher> selectAll() {
        return teacherMapper.selectAll();
    }

    @Override
    public Teacher selectByTid(Integer tid) {
        return teacherMapper.selectByTid(tid);
    }

}
